package org.supreme.basic.result;

import lombok.Data;
import org.supreme.basic.excetion.GlobExcetionEnum;
import org.supreme.basic.excetion.GlobalExcetion;

//返回JSON结果
@Data
//建造者模式
//@Builder
public class JSONResult {

    private boolean success = true;

    private String message = "成功";

    //错误码，用来描述错误类型 ，1000 表示么有错误
    private String code = "1000";

    //返回的数据
    private Object data;

    /** 创建当前实例 **/
    public static JSONResult success(){
        return new JSONResult();
    }
    /** 创建当前实例 **/
    public static JSONResult success(Object obj){
        JSONResult instance = new JSONResult();
        instance.setData(obj);
        return instance;
    }
    public static JSONResult ok(Object obj){
        JSONResult instance = new JSONResult();
        instance.setData(obj);
        return instance;
    }

    public static JSONResult success(Object obj,String code){
        JSONResult instance = new JSONResult();
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }
    public static JSONResult issuccess(Object obj,String message){
        JSONResult instance = new JSONResult();
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setData(obj);
        return instance;
    }
    /** 创建当前实例 **/

    public static JSONResult error(String message,String code){
        JSONResult instance = new JSONResult();
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setCode(code);
        return instance;
    }
    /** 枚举实列 **/
    public static JSONResult error(GlobExcetionEnum globExcetionEnum){
        JSONResult instance = new JSONResult();
        instance.setMessage(globExcetionEnum.getMessage());
        instance.setSuccess(false);
        instance.setCode(globExcetionEnum.getCode());
        return instance;
    }
    /** 当前自定义的异常 **/
    public static JSONResult error(GlobalExcetion globalExcetion){
        JSONResult instance = new JSONResult();
        instance.setMessage(globalExcetion.getMessage());
        instance.setSuccess(false);
        instance.setCode(globalExcetion.getCode());
        return instance;
    }

    public static JSONResult error(){
        JSONResult jsonResult = new JSONResult();
        jsonResult.setSuccess(false);
        return jsonResult;
    }

    /** 创建当前实例 **/
    public static JSONResult error(String message){
        return error(message,null);
    }

}
